<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台会员地址操作ajax
 * @package  ajax/shop/default
 * @author   allen.qiang
 */

quanli_require('module/base/customer/quanliCust.class.php');

class ajax_wexinKfPlugin {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //根据微信openId查询会员信息
                    $this->get();
                    break;

                case 2: //微信粉丝绑定会员
                    $this->bind();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    public function get() {

        global $G_SHOP;

        if (!isset($_REQUEST['openId']) || empty($_REQUEST['openId'])) {
            returnJsonData('参数错误');
        }

        //实例微电汇类
        $wdh_obj = $G_SHOP->eai('wdh');

        //实例会员类
        $quanliCust = new quanliCust();

        $wechat_customer_data = $wdh_obj->searchWechatCustomer($_REQUEST['openId'], '');
        if ($wechat_customer_data['code'] != 200) {
            returnJsonData('粉丝没有绑定会员', 10001);
        }

        //查询网站会员信息
        $customer_data = $quanliCust->searchCustomerDetail(array('pcustID' => $wechat_customer_data['data']['pcustID']));
        if (!$customer_data) {
            returnJsonData('粉丝没有绑定会员', 10001);
        }

        $return_data = $this->getReturnData($wechat_customer_data['data'], $customer_data);

        returnJsonData('ok', 200, $return_data);

    }

    private function bind() {

        global $G_SHOP;

        if (!isset($_REQUEST['openId']) || empty($_REQUEST['openId'])) {
            returnJsonData('缺少参数：openId');
        }
        if (!isset($_REQUEST['m']) || empty($_REQUEST['m'])) {
            returnJsonData('缺少参数：m');
        }

        //实例微电汇类
        $wdh_obj = $G_SHOP->eai('wdh');

        //实例会员类
        $quanliCust = new quanliCust();

        //查询网站会员信息
        $customer_data = $quanliCust->searchCustomerDetail(array('custID' => $_REQUEST['m']));
        if (!$customer_data) {
            returnJsonData('会员信息没有找到', 404);
        }

        //查询微信粉丝信息
        $wechat_customer_data = $wdh_obj->searchWechatCustomer($_REQUEST['openId'], '');
        if ($wechat_customer_data['code'] != 200) {
            returnJsonData('粉丝信息没有找到', 404);
        }

        //绑定会员信息
        $args_data = array(
            'wechat_uid' => $wechat_customer_data['data']['wechat_uid'],
            'pcustID' => $customer_data['pcustID'],
            'custID' => $customer_data['custID'],
            'custName' => $customer_data['custname'],
            'rankID' => $customer_data['rankID'],
            'rankName' => $customer_data['rankName'],
            'sumAmt' => $customer_data['sumAmt'],
            'custSource' => $customer_data['custSource'],
            'register_time' => $customer_data['createTime'],
            'mobile' => $customer_data['mobile'],
            'email' => $customer_data['email'],
        );
        $return_data = $wdh_obj->bindWechatCustomer($args_data);
        if ($return_data['code'] != 200) {
            returnJsonData('绑定失败');
        }

        $return_data = $this->getReturnData($wechat_customer_data['data'], $customer_data);

        returnJsonData('ok', 200, $return_data);

    }

    public function getReturnData($wechat_customer_data, $customer_data) {

        global $G_SHOP;

        $return_data = array();

        quanli_include('module/func/eai.func.php');
        quanli_include('module/base/bill/billSearch.class.php');
        quanli_include('module/base/promotion/quanliCoupon.class.php');
        quanli_include('module/base/bill/billReturn.class.php');
        quanli_include('module/base/bill/billRefund.class.php');
        quanli_include('module/base/customer/custRank.class.php');

        //实例crm类
        $crm_obj = eai_crm();

        //实例订单查询类
        $billSearch = new billSearch();

        //实例优惠券类
        $ebsigCoupon = new quanliCoupon();

        //实例退货类
        $billReturn = new billReturn( $G_SHOP->DBCA );

        //实例退款类
        $billRefund = new billRefund( $G_SHOP->DBCA );

        //实例会员等级类
        $custRank = new custRank();

        $pcustID = $customer_data['pcustID'];

        $customer_data['nickName'] = $wechat_customer_data['nickName'];

        /**
         * ===================
         * 获得会员积分
         * ===================
         */
        $result_array = $crm_obj->searchCustAccount( $customer_data['pcustID'], G_POINTS_ID );
        if ($result_array['code'] == 200) {
            $customer_data['points'] = $result_array['data']['useableAccount'];
        } else {
            $customer_data['points'] = 0;
        }

        /**
         * 会员余额
         */
        $result_array = $crm_obj->searchCustAccount($customer_data['pcustID'], G_CASH_ID);
        if($result_array['code'] == 200){
            $customer_data['balance'] = $result_array['data']['useableAccount'];
        } else {
            $customer_data['balance'] = 0;
        }

        /**
         * 会员等级折扣
         */
        $args_data = array(
            'rankID' => $customer_data['rankID']
        );
        $rank_data = $custRank->searchRank($args_data);
        $customer_data['rate'] = $rank_data[0]['rate'];
        $return_data['customer'] = $customer_data;

        //查询订单信息
        $args_data = array(
            'pcustID' => $pcustID,
            'page' => 1,
            'rp' => 5,
            'sortname' => 'bill_no',
            'sortorder' => 'DESC'
        );
        $bill_data = $billSearch->searchBillInfo( $args_data );
        if ($bill_data) {
            foreach($bill_data as $bill) {

                //判断配送时间是否为0
                $res = explode( ':',$bill['send_end_time']);
                $send_end_time_num = 0;
                foreach( $res as $val ){
                    if( intval($val) > 0 ){
                        $send_end_time_num = 1;
                    }
                }
                if( $send_end_time_num  ){
                    $arrive_time = $bill['send_date'].' '.$bill['send_start_time'] .'～'.$bill['send_end_time'];
                }else{
                    $arrive_time = $bill['send_date'].' '.$bill['send_start_time'];
                }

                $return_data['bill'][] = array(
                    'bill_no' => $bill['bill_no'],
                    'bill_code' => $bill['bill_code'],
                    'arrive_time' => $arrive_time,
                    'goods_name' => $bill['goods_name_str'],
                    'goods_amount' => $bill['bill_totalAmount']
                );
            }
        }

        //查询优惠券信息
        $args_data = array(
            'pcustID' => $pcustID
        );
        $coupon_data = $ebsigCoupon->searchCouponCode( $args_data );
        if ($coupon_data) {
            foreach( $coupon_data as $coupon ) {
                if (!empty($v['billNo'])) {
                    $return_data['coupon']['used'][] = $coupon;
                } else {
                    $past_sign = ($coupon['endDate'] < $G_SHOP->sys_date) ? true : false;
                    if ($past_sign) {
                        $return_data['coupon']['past'][] = $coupon;
                    } else {
                        $return_data['coupon']['no_use'][] = $coupon;
                    }
                }
            }
        }

        //查询退货单
        $args_data = array(
            'custID' => $customer_data['custID'],
            'page' => 1,
            'rp' => 5,
            'sortname' => 'return_no',
            'sortorder' => 'DESC'
        );
        $bill_return_data = $billReturn->searchBillReturn( $args_data );
        if ($bill_return_data) {
            foreach($bill_return_data as $bill_return) {
                $return_data['bill_return'][] = array(
                    'return_no' => $bill_return['return_no'],
                    'bill_no' => $bill_return['bill_no'],
                    'bill_code' => $bill_return['bill_code'],
                    'goods_name' => $bill_return['goods_name']
                );
            }
        }

        //查询退款单
        $args_data = array(
            'custID' => $customer_data['custID'],
            'page' => 1,
            'rp' => 5,
            'sortname' => 'refund_no',
            'sortorder' => 'DESC'
        );
        $bill_refund_data = $billRefund->searchBillRefund( $args_data );
        if ($bill_refund_data) {
            foreach($bill_refund_data as $bill_refund) {
                $return_data['bill_refund'][] = array(
                    'refund_no' => $bill_refund['refund_no'],
                    'bill_no' => $bill_refund['bill_no'],
                    'bill_code' => $bill_refund['bill_code'],
                    'refund_amount' => $bill_refund['refund_amount']
                );
            }
        }

        return $return_data;

    }

}